package com.ttdevs.ttrss.app;

import android.app.ActivityManager;
import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.ttdevs.ttrss.db.DBCnbetaManager;
import com.ttdevs.ttrss.db.DBManager;
import com.ttdevs.ttrss.db.IDBCnbetaManager;
import com.ttdevs.ttrss.db.IDBManager;
import com.ttdevs.ttrss.volley.BitmapLruCache;

public class AppInit {
	private static RequestQueue mRequestQueue;
	private static ImageLoader mImageLoader;
	private static IDBManager mDBManager;
	private static IDBCnbetaManager mDBCnbetaManager;

	private AppInit() {
		// no instances
	}

	static void init(Context context) {
		mRequestQueue = Volley.newRequestQueue(context);

		int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
		// Use 1/8th of the available memory for this memory cache.
		int cacheSize = 1024 * 1024 * memClass / 8;
		mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(cacheSize));

		mDBManager = DBManager.getInstance(context);
		mDBCnbetaManager = DBCnbetaManager.getInstance(context);
	}

	public static RequestQueue getRequestQueue() {
		if (mRequestQueue != null) {
			return mRequestQueue;
		} else {
			throw new IllegalStateException("RequestQueue not initialized");
		}
	}

	public static ImageLoader getImageLoader() {
		if (mImageLoader != null) {
			return mImageLoader;
		} else {
			throw new IllegalStateException("ImageLoader not initialized");
		}
	}

	public static IDBManager getDBManager() {
		if (mDBManager != null) {
			return mDBManager;
		} else {
			throw new IllegalStateException("DBManager not initialized");
		}
	}

	public static IDBCnbetaManager getDBCnbetaManager() {
		if (mDBCnbetaManager != null) {
			return mDBCnbetaManager;
		} else {
			throw new IllegalStateException("DBCnbetaManager not initialized");
		}
	}
}
